<?php
  session_start();
  date_default_timezone_set('PRC');
  define('ROOT_PATH', str_replace('includes/init.php', '', str_replace('\\', '/', __FILE__)));  
  require(ROOT_PATH.'/includes/cls_template.php');
  require(ROOT_PATH.'/includes/class.phpmailer.php');
  require(ROOT_PATH.'/includes/serverList.php');
  require(ROOT_PATH.'/includes/sendEmail.php');
  require(ROOT_PATH.'/includes/action.php');
  require(ROOT_PATH.'/includes/configGlobal.php');
  require(ROOT_PATH.'/includes/page.php');  
  require(ROOT_PATH.'/includes/client.php');
  require(ROOT_PATH.'/includes/name.php');
  $GLOBALS['db'] = new mysqls("send");
  //$db = new mysqls("send");
   //每次发送的数量
      
  if(date("w")==0||date("w")==6){
  	//周六周日的发送数量
       $d3=substr(date("Y-m-d H:i:s"),11,2);
       if($d3>0 && $d3<07){
           $num = rand(1,1);
       }elseif($d3>07 && $d3<09){
           $num = rand(1,2);
       }elseif($d3>09 && $d3<18){
           $num = rand(1,2);
       }else{
           $num = rand(1,1);
       }
  }else{
  	//周一周五的发送数量
	$d3=substr(date("Y-m-d H:i:s"),11,2);
       if($d3>0 && $d3<07){
           $num = rand(1,2);
       }elseif($d3>07 && $d3<09){
           $num = rand(1,2);
       }elseif($d3>09 && $d3<18){
           $num = rand(1,2);
       }else{
           $num = rand(1,2);	
       }
  }
//每次发送的数量  



//给发送记录日志
//numTable  定义NUM表名称
//templateId 定义模板ID
//type 定义生成送模板EMAIL的类型比如:YAHOO为1,GMAIL为2,HOTMAIL为3
function sendLog($numTable, $templateId, $type){
    $sql = "update ".$numTable." set num = num +1 where templateId = ".$templateId." and emailNumType = ".$type."";
    $GLOBALS['db']->dbquery($sql);
	//更新模板
	$sql = "select num from num where num >=1 and templateId = ".$templateId." and emailNumType = ".$type."";
	$rowResult = $GLOBALS['db']->dbGetRow($sql);
	//echo $sql;
//	print_r($rowResult);exit;
	//如果大于指定的数,就更换对应模板
	if($rowResult[0]>$GLOBALS['templateNum']){
		$sql = "update ".$numTable." set flag = 1 where templateId = ".$templateId." and emailNumType = ".$type."";
		$GLOBALS['db']->dbquery($sql);	
	}
}
//给发送记录日志
//给自己的SMTP分配模板
function insertIntoNum($emailTypeArr, $templateId){
	foreach($emailTypeArr as $key=>$value){
		$sql = "insert into num(templateId, emailNumType, timeStamps) values(".$templateId.", ".$key.", ".time().")";
		$GLOBALS['db']->dbquery($sql);
	}
}
//给第三方SMTP分配模板
function insertIntoThirdPartNum($emailThirdTypeArr, $templateId){
	foreach($emailThirdTypeArr as $key=>$value){
		$sql = "insert into num(templateId, emailNumType, timeStamps) values(".$templateId.", ".$key.", ".time().")";
		$GLOBALS['db']->dbquery($sql);
	}
}
function createNum()
{
  $timeStr = mktime(0, 0, 0, 8, 9, 2013);
 // echo $timeStr."<Br/>";
    for ($i=0; $i < 3600; $i++) { 
      $timeStrs = intval($timeStr)+$i*86400;
      $formatTime = date("Y-m-d H:i:s", $timeStrs);
      echo $formatTime."--".$timeStrs."<br/>";
     $sql = "insert into num(num, timeStamp) values(0, ".$timeStrs.")";
     $GLOBALS['db']->dbquery($sql);
    }
}

function getTempTemplateNum(){
	$sql = "select count(*) as num from(select distinct(address) from tempTemplate) as ab";
	$Num = $GLOBALS['db']->dbGetRow($sql);
	return $Num;
}

function getOpenRateNum($email = NULL,$recordSet = NULL){
  if($recordSet == NULL){
	  if($email == NULL){
		  $sql = "select count(*) as num from(select distinct(address) from openStatistics) as ab";
	  }else{
		  $sql = "select count(address) as num from openStatistics where address = '".$email."' group by address";
	  }
   }else{
	  if($email == NULL){
		  $sql = "select count(*) as num from(select distinct(address) from openStatistics where type = ".$recordSet.") as ab";
	  }else{
		  $sql = "select count(address) as num from openStatistics where address = '".$email."' and type = $recordSet group by address";
	  }   
   }
//   echo $sql;
  $Num = $GLOBALS['db']->dbGetRow($sql);
  return $Num;
}

function getClickRateNum($email = NULL,$recordSet = NULL){
  if($recordSet == NULL){
	  if($email == NULL){
		  $sql = "select count(*) as num from(select distinct(address) from clickTrack) as ab";
	  }else{
		  $sql = "select count(address) as num from clickTrack where address = '".$email."' group by address";
	  }
  }else{
	  if($email == NULL){
		  $sql = "select count(*) as num from(select distinct(address) from clickTrack where type = ".$recordSet.") as ab";
	  }else{
		  $sql = "select count(address) as num from clickTrack where address = '".$email."' and type = $recordSet group by address";
	  }  
  }
  $Num = $GLOBALS['db']->dbGetRow($sql);
  return $Num;
}

function getUnsubRateNum(){
  $sql = "select count(*) as num from unsub";
  $Num = $GLOBALS['db']->dbGetRow($sql);
  return $Num;
}

function getUpdateEmailNum(){
  $sql = "select count(*) as num from updateEmail";
  $Num = $GLOBALS['db']->dbGetRow($sql);
  return $Num;
}

function showAllUpdate($pageNum = NULL){
	$prePage = ($pageNum-1)*$GLOBALS['pageSize'];
    $sql = "select * from updateEmail order by id desc limit $prePage,".$GLOBALS['pageSize']."";
	$result = $GLOBALS['db']->dbfetchAll($sql);
	return $result;
}

function checkUnsubRate($id = NULL,$pageNum = NULL,$email=NULL){
    if (empty($id)) {
      if(empty($pageNum)){
			$sql = "select count(email) as num,id,email,emailId,userIp,timeStamps,userBrowser,type,flag from unsub group by address order by id desc limit 0,1";
			$resultArr = $GLOBALS['db']->dbfetchAll($sql);		
      }else{
	    if(!empty($email)){
			$sql = "select * from unsub where email = '".$email."'";
			$resultArr = $GLOBALS['db']->dbfetchAll($sql);
		}else{
          $prePage = ($pageNum-1)*$GLOBALS['pageSize'];
          $sql = "select count(email) as num,id,email,emailId,userIp,timeStamps,userBrowser,type,flag from unsub group by email order by id desc limit $prePage,".$GLOBALS['pageSize']."";
          $resultArr = $GLOBALS['db']->dbfetchAll($sql);
		}		  
      }
    }else{
      if (is_numeric($id)) {
        $sql = "select * from unsub where id =".$id."";
        $resultArr = $GLOBALS['db']->dbquery($sql);
      }else{
        die("id formate is error");
      }
    }
    return $resultArr;
}

function checkClickRate($id = NULL,$pageNum = NULL,$email=NULL,$recordSet=NULL){
    if (empty($id)) {
      if(empty($pageNum)){
			$sql = "select count(address) as num,id,url,address,addressId,ip,type,timeStamps,brownserLanguage,country,province,city from clickTrack group by address order by id desc limit 0,1";
			$resultArr = $GLOBALS['db']->dbfetchAll($sql);		
      }else{
	      $prePage = ($pageNum-1)*$GLOBALS['pageSize'];
	      if($recordSet == NULL){
			  if(!empty($email)){
				  $sql = "select * from clickTrack where address = '".$email."' limit $prePage,".$GLOBALS['pageSize']."";
				  $resultArr = $GLOBALS['db']->dbfetchAll($sql);
			  }else{
			    $sql = "select count(address) as num,id,url,address,addressId,ip,type,timeStamps,brownserLanguage,country,province,city from clickTrack group by address order by id desc limit $prePage,".$GLOBALS['pageSize']."";  
			  }
		}else{
			  if(!empty($email)){
				  $sql = "select * from clickTrack where address = '".$email."' and type = ".$recordSet." limit $prePage,".$GLOBALS['pageSize']."";
				  $resultArr = $GLOBALS['db']->dbfetchAll($sql);
			  }else{
			    $sql = "select count(address) as num,id,url,address,addressId,ip,type,timeStamps,brownserLanguage,country,province,city from clickTrack where type = $recordSet group by address order by id desc limit $prePage,".$GLOBALS['pageSize']."";  
			  }
		}
		$resultArr = $GLOBALS['db']->dbfetchAll($sql);	  
      }
    }else{
      if (is_numeric($id)) {
        $sql = "select * from clickTrack where id =".$id."";
        $resultArr = $GLOBALS['db']->dbquery($sql);
      }else{
        die("id formate is error");
      }
    }
    return $resultArr;
}


function checkOpenRate($id = NULL,$pageNum = NULL,$email=NULL,$recordSet=NULL){
    if (empty($id)) {
      if(empty($pageNum)){
	      if($recordSet == NULL){
		      $sql = "select count(address) as num,id,address,addressId,ip,type,timeStamps,brownserType,brownserLanguage,country,province from openStatistics group by address order by id desc limit 0,1";
		  }else{
		  	  $sql = "select count(address) as num,id,address,addressId,ip,type,timeStamps,brownserType,brownserLanguage,country,province from openStatistics where type = $recordSet group by address order by id desc limit 0,1";
		  }	
		  $resultArr = $GLOBALS['db']->dbfetchAll($sql);
      }else{
	      if($recordSet == NULL){
		      $prePage = ($pageNum-1)*$GLOBALS['pageSize'];
			  if(!empty($email)){
			      $sql = "select * from openStatistics where address = '".$email."' order by id desc limit $prePage,".$GLOBALS['pageSize']."";
				  $resultArr = $GLOBALS['db']->dbfetchAll($sql);
			  }else{
				  $sql = "select count(address) as num,id,address,addressId,ip,type,timeStamps,brownserType,brownserLanguage,country,province from openStatistics group by address order by id desc limit $prePage,".$GLOBALS['pageSize']."";
			  }
		  }else{
		      $prePage = ($pageNum-1)*$GLOBALS['pageSize'];
			  if(!empty($email)){
			      $sql = "select * from openStatistics where address = '".$email."' and type = $recordSet order by id desc limit $prePage,".$GLOBALS['pageSize']."";
				  $resultArr = $GLOBALS['db']->dbfetchAll($sql);
			  }else{
				  $sql = "select count(address) as num,id,address,addressId,ip,type,timeStamps,brownserType,brownserLanguage,country,province from openStatistics where type = $recordSet group by address order by id desc limit $prePage,".$GLOBALS['pageSize']."";
			  }		  
		  }
		  $resultArr = $GLOBALS['db']->dbfetchAll($sql);	  
      }
    }else{
      if (is_numeric($id)) {
        $sql = "select * from openStatistics where id =".$id."";
        $resultArr = $GLOBALS['db']->dbquery($sql);
      }else{
        die("id formate is error");
      }
    }
    return $resultArr;
}

function checkTempTemplate($id = NULL,$pageNum = NULL){
    if (empty($id)) {
  	  if(empty($pageNum)){
  		  $sql = "select id, address, title, type, flag from tempTemplate order by id desc limit 0,1";
  		  $resultArr = $GLOBALS['db']->dbfetchAll($sql);
  	  }else{
  	     // $nextPage = $pageNum*$pageNo;
  		    $prePage = ($pageNum-1)*$GLOBALS['pageSize'];
  	  	  $sql = "select id, address, title, type, flag from tempTemplate order by id desc limit $prePage,".$GLOBALS['pageSize']."";
  		    $resultArr = $GLOBALS['db']->dbfetchAll($sql);
  	  }
    }else{
      if (is_numeric($id)) {
        $sql = "select * from tempTemplate where id =".$id."";
        $resultArr = $GLOBALS['db']->dbquery($sql);
      }else{
        die("id formate is error");
      }
    }
    return $resultArr;
}

function delTempTemplate($id){
    if (is_numeric($id)) {
        $sql = "delete from tempTemplate where id = ".$id." ";
        $GLOBALS['db']->dbquery($sql);
    }
}

function viewTempTemplate($id){
  if (is_numeric($id)) {
      $sql = "select * from tempTemplate where id = ".$id." ";
      $rowArr = $GLOBALS['db']->dbGetRow($sql);
  }else{
      die("param error");
  }
  return $rowArr;
}

//清除temptemplate表
function clearTempTemplate(){
	$sql = "truncate tempTemplate";
	$GLOBALS['db']->dbquery($sql);
	return true;
}

function showAllUnsub(){
    $sql = "select * from unsub where flag = 0;";
	$result = $GLOBALS['db']->dbfetchAll($sql);
	return $result;
}


function delOk(){
	$sql = "delete from tempTemplate where flag = 1";
	$GLOBALS['db']->dbquery($sql);
}

function getSendServer($emailType){
    $num = '';$result = '';$serverNum = '';$rnum = '';
	$serverArr = array();
	$sql = "select * from sendLimit where emailType = ".$emailType." and flag = 0";
	$resultArr = $GLOBALS['db']->dbfetchAll($sql);
	foreach($resultArr as $key => $value){
	    if($value['emailSendNum']>$value['emailNumLimit']){
			$sql = "update sendLimit set flag = 1 where id = ".$value['id']."";
			$GLOBALS['db']->dbquery($sql);
		}else{
		    $serverArr[] = $value['ipType'];
			$num = count($serverArr);
			if($num == 0){
			    $result = "";
				exit;
			}else{
			    $num = $num - 1;
				$rnum = rand(0, $num);
				$result = $serverArr[$rnum];
			}
		}
	}
	return $result;
}

function sendEmailLog($ipType, $emailType){
	$sql = "update sendLimit set emailSendNum = emailSendNum + 1 where ipType = ".$ipType." and emailType = ".$emailType."";
	$GLOBALS['db']->dbquery($sql);
}

function thirdPartSendLog($type){
	$sql = "update thirdPartSend set num = num + 1 where typeId = ".$type."";
	$GLOBALS['db']->dbquery($sql);
}

function formateTime(){
	$timeStr = date("Y-m-d H:i:s", time());
	$timeArr = explode(" ",$timeStr);
	$resultArr = explode("-", $timeArr[0]);
	$times = mktime(0,0,0,$resultArr[1],$resultArr[2],$resultArr[0]);
	return $times;
}
?>